layui.use(['jquery', 'layer', 'table'], function(){
    var $ = layui.jquery,
        layer = layui.layer,
        table = layui.table;

    //渲染表格
    table.render({
        id: 'spiderList',
        elem: '#spiderList',
        url:'../spider/spiderQuery',
        toolbar: '#tableTopTmp',
        defaultToolbar:['filter', 'print', 'exports'],
        cols: [
            [
                {title: '序号',width:'58',align: "center",templet: "#indexTable"},
                {field: 'spiderProcess', width: '25%', title: '爬虫Process', align: 'center'},
                {field: 'beginUrl', width: '25%', title: '初始链接', align: 'center'},
                {field: 'spiderArea', width: '10%', title: '爬虫爬取行政区域', align: 'center'},
                {field: 'lastRunTime', width: '15%', title: '上次爬取时间', align: 'center'},
                {field: 'pageCount', width: '5%', title: '爬取页数', align: 'center'},

                {fixed: 'right', title: '操作', width:'14%', align: 'center', toolbar: '#spiderListTool'}
            ]
        ],
        page: true,
        even: true,
        limit: 10,
        done: function(){
            table.resize('spiderList');
        }
    });

    //监听工具条
    table.on('tool(spiderList)', function (obj) {
        var data = obj.data;
        var spiderId = data.id;
       if(obj.event === 'edit'){  //编辑
            var pageUrl = '../spider/spiderCreate?pageType=1&spiderId='+spiderId;
            var index = layer.open({
                title:' ',
                type: 2,
                maxmin: true,
                skin: 'larry-green',
                shade: 0.3,
                resize: true,
                area: ['90%', '90%'],
                content: pageUrl,
                success:function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                },
                end: function () {
                    table.reload('spiderList');
                },
                min: function () {
                    $(".layui-layer-title").text("修改爬虫管理表");
                },
                restore: function () {
                    $(".layui-layer-title").text(" ");
                }
            });
        } else if(obj.event === 'del'){    //删除
            layer.confirm('确定要删除该爬虫管理表吗?', {icon: 3}, function (index) {
                var loadingIndex = layer.load(1, {time: 10*1000,shade: 0.1});
                $.post("../spider/spiderDel", {spiderId: data.id}, function (result) {
                    if (result.code == 0) {
                        layer.msg('删除成功！', {icon: 1, time: 1500,shade: 0.1}, function () {
                            table.reload("spiderList");
                        });
                    } else {
                        if (result.msg) {
                            layer.msg("处理失败！<br/><div style='color:red;'>错误信息：" + result.msg + "</div>", {
                                icon: 2,
                                time: 2000
                            });
                        } else {
                            layer.msg('删除失败！', {icon: 2, time: 2000,shade: 0.1});
                        }
                    }
                    layer.close(loadingIndex);
                })
            }, function (index) {
                //取消
            });
        }else if(obj.event === 'run'){
           layer.confirm('确定要运行该爬虫吗?', {icon: 3}, function (index) {
               var loadingIndex = layer.load(1, {time: 10*1000,shade: 0.1});
               $.post("../spider/spiderRun", {spiderId: data.id}, function (result) {
                   if (result.code == 0) {
                       layer.msg('运行完成', {icon: 1, time: 1500,shade: 0.1}, function () {
                           table.reload("spiderList");
                       });
                   } else {
                       if (result.msg) {
                           layer.msg("处理失败！<br/><div style='color:red;'>错误信息：" + result.msg + "</div>", {
                               icon: 2,
                               time: 2000
                           });
                       } else {
                           layer.msg('删除失败！', {icon: 2, time: 2000,shade: 0.1});
                       }
                   }
                   layer.close(loadingIndex);
               })
           }, function (index) {
               //取消
           });
       }
       // else  if(obj.event === 'upload'){  //编辑
       //     var pageUrl = '../admin/spiderUpload?spiderId='+spiderId;
       //     var index = layer.open({
       //         title:' ',
       //         type: 2,
       //         maxmin: true,
       //         skin: 'larry-green',
       //         shade: 0.3,
       //         resize: true,
       //         area: ['40%', '60%'],
       //         content: pageUrl,
       //         success:function (layero, index) {
       //             var body = layer.getChildFrame('body', index);
       //         },
       //         end: function () {
       //             table.reload('spiderList');
       //         },
       //         min: function () {
       //             $(".layui-layer-title").text("上传图片");
       //         },
       //         restore: function () {
       //             $(".layui-layer-title").text(" ");
       //         }
       //     });
       // }

    });

    //监听头部工具栏事件
    //监听事件
    table.on('toolbar(spiderList)', function(obj){
        switch(obj.event){
            case 'getSelect':
                var queryName = $("#queryName").val();
                table.reload('spiderList', {
                    where: {
                        queryName: queryName
                    }
                });
                break;
            case 'projectAdd':
                var pageUrl = '../spider/spiderCreate?pageType=0';
                var index = layer.open({
                    title:' ',
                    type: 2,
                    maxmin: true,
                    skin: 'larry-green',
                    shade: 0.3,
                    resize: true,
                    area: ['90%', '90%'],
                    content: pageUrl,
                    success:function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                    },
                    end: function () {
                        table.reload('spiderList');
                        $('#spiderId').val('');
                    },
                    min: function () {
                        $(".layui-layer-title").text("新增资源");
                    },
                    restore: function () {
                        $(".layui-layer-title").text(" ");
                    }
                });
                break;
        }
    });
});